//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service.tool.eway;

import com.eway.payment.rapid.sdk.RapidClient;
import com.eway.payment.rapid.sdk.RapidSDK;
import com.eway.payment.rapid.sdk.beans.external.*;
import com.eway.payment.rapid.sdk.output.CreateTransactionResponse;
import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.util.PropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class EwayServiceImpl implements EwayService {

    private final String apiKey = PropertiesUtil.getProperty("EWAY_APIKEY");
    private final String password = PropertiesUtil.getProperty("EWAY_PASSWORD");
    private final String rapidEndpoint = PropertiesUtil.getProperty("EWAY_RAPIDENDPOINT");

    @Override
    public Map toPay(String name, String number, String expiryMonth, String expiryYear, String CVN, int totalAmount) {
        Map map = Maps.newHashMap();
        RapidClient client = RapidSDK.newRapidClient(apiKey, password, rapidEndpoint);

        CardDetails cardDetails = new CardDetails();
        cardDetails.setName(name);
        cardDetails.setNumber(number);
        cardDetails.setExpiryMonth(expiryMonth);
        cardDetails.setExpiryYear(expiryYear);
        cardDetails.setCVN(CVN);

        Customer customer = new Customer();
        customer.setCardDetails(cardDetails);

        PaymentDetails paymentDetails = new PaymentDetails();
        paymentDetails.setTotalAmount(totalAmount);

        Transaction transaction = new Transaction();
        transaction.setCustomer(customer);
        transaction.setPaymentDetails(paymentDetails);
        transaction.setTransactionType(TransactionType.Purchase);

        PaymentMethod method = PaymentMethod.Direct;
        CreateTransactionResponse response = client.create(method, transaction);

        //判断交易状态和状态码是否通过
        if (response.getTransactionStatus().isStatus() && "00".equals(response.getTransactionStatus().getProcessingDetails().getResponseCode())) {
            map.put("code", Const.SUCCESS_CODE);
            map.put("transactionId", response.getTransactionStatus().getTransactionID());
            System.out.println("Transaction successful! ID: " + response.getTransactionStatus().getTransactionID());
            return map;
        }else{
            map.put("code", Const.ERROR_CODE);
            return map;
        }
    }
}
